//
//  StatusRetweetedView.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
  
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 设置UI界面
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.90, alpha: 1)
        
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedLabel.snp_left)
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //给转发微博添加底部约束
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstranit = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载子视图
    private lazy var retweetedLabel: UILabel = UIFactory.labelFactory("北京的地方不好混", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
    
    //声明约束属性 对象类型是Constraint
    var bottomConstranit: Constraint?
    
    //数据模型
    var retweetedStatus: Status? {
        
        didSet {
            retweetedLabel.text = retweetedStatus?.text
//            pictureView.imageURLs = retweetedStatus?.imageURLs
            
            
            //在动态更新约束之前 卸载约束 在cell复用下 约束也被复用
            bottomConstranit?.uninstall()
            
            //给配图视图数据源赋值
            //转发微博没有配图视图 数组不能为空
            //            print(status?.imageURLs?.count)
            
            if let imgURLs = retweetedStatus?.imageURLs where imgURLs.count != 0 {
                //                print("有配图视图")
                
                //显示配图视图
                pictureView.hidden = false
                pictureView.imageURLs = imgURLs
                //                pictureView.imageURLs = status?.imageURLs
                
                //修改转发微博底部约束 相对于配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstranit = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            } else {
                //                print("-----没有配图视图----")
                
                //隐藏配图视图
                pictureView.hidden = true
                
                pictureView.imageURLs = nil
                //相对于微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstranit = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    
    }
    
}



















